package ship;

import java.lang.reflect.InvocationTargetException;
import java.util.Hashtable;
import java.util.Vector;

import main.Game;

import secretweapons.Pulse;
import structure.SObject;
import util.Timer;
import weapons.StaticMine;
import weapons.Weapon;
import weapons.WeaponConstants;

/**
 * This class is the weapon manager for the ship class.  The timers are cooldown timers, and should be set
 * based on information in WeaponConstants.  Num contains info on how many of each weapon is availible.
 * 
 * This class should be able to change which weapon is in what slot (which restarts the cooldown), get the list
 * of possible weapons (if num(weapon) is 0, should not show up in the list), and fire from left, right, or special,
 * which should decrease num and reset cooldown.
 * 
 * Firing should have a giant switch statement on it, aiming weapons at Game.game.getTarget().
 * @author max
 *
 */

public class Arsenal {

	private Hashtable<String, Timer> timers = new  Hashtable<String, Timer>();
	private Vector<Class> weapons = new Vector<Class>();
	private Hashtable<String, Integer> num = new Hashtable<String, Integer>();
	private Vector<Class> specials = new Vector<Class>();

	private Class<Weapon> right;
	private Class<Weapon> left;
	private Class<Weapon> special;

	private WeaponConstants constants = new WeaponConstants();

	public Arsenal() {
		num.putAll(WeaponConstants.load);
		timers.putAll(WeaponConstants.timers);
		weapons.addAll(WeaponConstants.weapons);
		specials.addAll(WeaponConstants.specials);
		left = weapons.get(0);
		right = weapons.get(0);
		special = specials.get(0);
	}

	public void fire(int tube) {
		Ship ship = Game.game.getShip();
		Class<Weapon> toFire;
		if(tube == WeaponConstants.CENTER) {
			toFire = special;
			if(Game.game.getTarget() == null) {
				return;
			}
		}
		else if(tube == WeaponConstants.LEFT) {
			toFire = left;
			if(Game.game.getTarget() == null) {
				return;
			}
		}
		else {
			toFire = right;
		}
		Object[] shipArray = { ship };
		try {
			Game.game.registerAll((SObject) toFire.getConstructors()[0].newInstance(shipArray));
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			System.out.println(e.getCause());
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


		timers.get(toFire.getName()).reset();
		timers.get(toFire.getName()).start();
		num.put(toFire.getName(), num.get(toFire.getName())-1);
	}


	/**
	 * Returns precent ready of the  weapon.
	 * @return
	 */
	public double getRightTime() {
		return timers.get(right).precentDone();
	}

	public double getLeftTime() {
		return timers.get(left).precentDone();
	}

	public double getSpecialTime() {
		return timers.get(special).precentDone();
	}


	public void changeLeft(int index) {
		if(! weapons.get(index).equals(left)) {
			left = weapons.get(index);
			timers.get(left.getName()).reset();
			timers.get(left.getName()).start();
		}
	}
	public void changeRight(int index) {
		if(! weapons.get(index).equals(right)) {
			right = weapons.get(index);
			timers.get(right.getName()).reset();
			timers.get(right.getName()).start();
		}
	}
	public void changeCenter(int index) {
		if(! specials.get(index).equals(special)) {
			special = specials.get(index);
			timers.get(special.getName()).reset();
			timers.get(special.getName()).start();
		}
	}
	

	public int getLeft() {
		return weapons.indexOf(left);
	}
	public int getRight() {
		return weapons.indexOf(right);
	}
	public int getSpecial() {
		return specials.indexOf(special);
	}

}
